package com.yanqu.collectclientdata.web;

import com.yanqu.collectclientdata.db.CollectClientDbManager;
import com.yanqu.collectclientdata.timer.CollectClientDataTimer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.TempServer;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class CollectClientDataServer extends TempServer {

    private static final Logger logger = LogManager.getLogger(CollectClientDataServer.class.getName());

    private static CollectClientDataServer collectClientDataServer;

    private CollectClientDataServer() {

    }

    public static CollectClientDataServer getInstance() {
        if (collectClientDataServer == null) {
            collectClientDataServer = new CollectClientDataServer();
        }
        return collectClientDataServer;
    }

    public boolean start() {
        // 初始化数据库
        CollectClientDbManager.initDb();

        // 初始化定时器
        CollectClientDataTimer.init();
        CollectClientDataTimer.startTask();

        //注册异常关闭处理过程
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            logger.error("CollectServer kill 关闭服务器");
            CollectClientDataServer.getInstance().newStop();
        }));

        return true;
    }

    public boolean newStop() {
        try {
            CollectClientDataTimer.stopTask();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

        }

        return true;
    }

	public List<TempMgr> getMgrList() {
		List<TempMgr> mgrList = new ArrayList<>();
		return mgrList;
	}

}
